#import "XMShopViewCell.h"

#define kImageSize 80
#define kHorizontalMargin 15
#define kVerticalMargin 10
#define kButtonWidth 120
#define kButtonHeight 40
#define kCellSpacing 8 // 新增：cell间距

@implementation XMShopViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.backgroundColor = kColorHEX(0xF0F0F0);
         self.bgView.frame = self.contentView.bounds;
        [self.contentView addSubview:self.bgView];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    
    // 产品图片
    _productImageView = [[UIImageView alloc] init];
    _productImageView.contentMode = UIViewContentModeScaleAspectFill;
    _productImageView.layer.cornerRadius = 8;
    _productImageView.layer.masksToBounds = YES;
    [self.bgView addSubview:_productImageView];
    
    // 产品名称
    _productNameLabel = [[UILabel alloc] init];
    _productNameLabel.numberOfLines = 0;
    _productNameLabel.font = [UIFont systemFontOfSize:16];
    [self.bgView addSubview:_productNameLabel];
    
    // 产品价格
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.bgView addSubview:_priceLabel];
    
    // 下单按钮
    _orderButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _orderButton.backgroundColor = [UIColor systemYellowColor];
    _orderButton.layer.cornerRadius = 20;
    _orderButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_orderButton setTitle:@"去下单" forState:UIControlStateNormal];
    [_orderButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.bgView addSubview:_orderButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat contentX = kHorizontalMargin;
    CGFloat contentY = kCellSpacing / 2;
    CGFloat contentWidth = self.contentView.bounds.size.width;
    CGFloat contentHeight = self.frame.size.height - kCellSpacing;
    self.bgView.frame = CGRectMake(0, 0, kScreenWidth, self.contentView.bounds.size.height - 10);
    
    // 布局产品图片
    _productImageView.frame = CGRectMake(kHorizontalMargin, kVerticalMargin, kImageSize, kImageSize);
    
    // 布局产品名称
    CGFloat nameX = CGRectGetMaxX(_productImageView.frame) + kHorizontalMargin;
    CGFloat nameY = kVerticalMargin;
    CGFloat nameWidth = self.contentView.bounds.size.width - nameX - kHorizontalMargin - kButtonWidth;
    _productNameLabel.frame = CGRectMake(nameX, nameY / 2, nameWidth, self.contentView.bounds.size.height - 2 * kVerticalMargin);
    
    // 布局产品价格
    CGFloat priceX = kHorizontalMargin;
    CGFloat priceY = CGRectGetMaxY(_productImageView.frame) + kVerticalMargin;
    _priceLabel.frame = CGRectMake(priceX, priceY, self.contentView.bounds.size.width / 2, 20);
    
    // 布局下单按钮
    CGFloat buttonX = self.contentView.bounds.size.width - kHorizontalMargin - kButtonWidth;
    CGFloat buttonY = (self.contentView.bounds.size.height - kButtonHeight) / 2;
    _orderButton.frame = CGRectMake(buttonX, priceY, kButtonWidth, kButtonHeight);
}

- (void)configureWithImage:(UIImage *)image name:(NSString *)name price:(NSString *)price {
    _productImageView.image = image;
    _productNameLabel.text = name;
    _priceLabel.text = price;
}

@end
